import { _decorator, Component, Node } from 'cc';
import { BaseTs } from '../../Prefabs/Scripts/BaseTs';
// import CryptoJS from 'crypto-js';
const { ccclass, property } = _decorator;

@ccclass('HttpUtil')
export class HttpUtil extends BaseTs {

    public static INSTANCE: HttpUtil = null;

    protected onLoad(): void {
        if (HttpUtil.INSTANCE == null) {
            HttpUtil.INSTANCE = this
        }
    }


    /**
     * 发送post请求
     * @param requestUrl 请求url
     * @param data 请求参数
     * @param callFun 回调
     */
    public post(requestUrl: string, data: any, callFun: any) {
        this.postJson(requestUrl, JSON.stringify({ "data": data }), (err, data) => {
            console.log('err', err);
            console.log('data', data);
        })
    }

    /**
     * 发送get请求
     * @param url 请求URL
     * @param params 参数信息 a=1&b=2
     * @param responseCall 执行成功后的回调
     */
    private getJson(url: string, params: string, responseCall: Function): void {
        var xhr = new XMLHttpRequest();
        xhr.timeout = 5000;
        var requestURL = url;
        if (params) {
            requestURL = requestURL + "?" + params;
        }
        xhr.open("GET", requestURL, true);
        xhr.setRequestHeader("Authorization", "GlobalVar.sign");
        this.setResponsOpt(xhr, responseCall);
        xhr.send();
    }

    /**
     * 发送Post请求
     * @param url 请求URL
     * @param jsonBody 请求体
     * @param responseCall 执行成功后的回调 
     */
    private postJson(url: string, jsonBody: string, responseCall: Function) {
        var xhr = new XMLHttpRequest();
        xhr.timeout = 5000;

        var requestURL = url;

        xhr.open("POST", requestURL, true);


        xhr.setRequestHeader('Content-Type', 'application/json');
        xhr.setRequestHeader('Accept', 'application/json');


        xhr.setRequestHeader("Authorization", 'UUID_IIIIDDDD');

        this.setResponsOpt(xhr, responseCall);
        xhr.send(jsonBody);
    }


    /**
     * 设置请求参数信息
     * @param xhr Ajax
     * @param responseCall 请求回调 
     */
    private setResponsOpt(xhr: XMLHttpRequest, responseCall) {
        xhr.onreadystatechange = function () {
            if (xhr.readyState === 4) {
                // console.log("http res("+ xhr.responseText.length + "):" + xhr.responseText);
                if ((xhr.status >= 200 && xhr.status < 300)) {
                    let dataRespones = null;
                    if (xhr.responseType === "text" || xhr.responseType === "json") {
                        dataRespones = xhr.responseText;
                    }
                    else {
                        dataRespones = xhr.response;
                    }
                    if (responseCall !== null) {
                        responseCall(null, dataRespones);
                    }
                }
                else {
                    responseCall(xhr.readyState + ":" + xhr.status, null);
                }
            }
        };

        xhr.onerror = function (err) {
            if (responseCall) {
                responseCall(err, null);
            }
        }

        xhr.ontimeout = function () {
            if (responseCall) {
                responseCall("Timeout", null);
            }
        }
    }

    // public encryptAes(contentJson, keyStr) {
    //     keyStr = keyStr ? keyStr : this.getAesKey();
    //     var ivStr = keyStr;
    //     let key = CryptoJS.enc.Utf8.parse(keyStr);
    //     let iv = CryptoJS.enc.Utf8.parse(ivStr);
    //     // 对contentPo转成JSON串
    //     let srcs = CryptoJS.enc.Utf8.parse(contentJson);
    //     console.log("keyStr", keyStr)
    //     console.log("iv", iv)
    //     console.log("contentJson", contentJson)
    //     let encrypted = CryptoJS.AES.encrypt(srcs, key, {
    //         iv,
    //         mode: CryptoJS.mode.CBC,
    //         padding: CryptoJS.pad.Pkcs7
    //     });
    //     return encrypted.toString();
    // }

    // public decrypt(contentJson, keyStr) {
    //     keyStr = keyStr ? keyStr : this.getAesKey();
    //     var ivStr = keyStr;
    //     var key = CryptoJS.enc.Utf8.parse(keyStr);
    //     let iv = CryptoJS.enc.Utf8.parse(ivStr);

    //     var decrypt = CryptoJS.AES.decrypt(contentJson, key, {
    //         iv,
    //         mode: CryptoJS.mode.CBC,
    //         padding: CryptoJS.pad.Pkcs7
    //     });
    //     return decrypt.toString(CryptoJS.enc.Utf8);
    // }

    // public static Md5(input: string) {
    //     return CryptoJS.MD5(input).toString();
    // }

    // //获取加密key值
    // public getAesKey() {
    //     var keyStr = "zhizhudianjing!@";
    //     return "fd33845bef414845";
    // }
}